package com.example.elderlycare.controller;

import com.example.elderlycare.controller.entity.BookRecordRequestBody;
import com.example.elderlycare.dao.entity.BookRecord;
import com.example.elderlycare.service.BookRecordService;
import com.example.elderlycare.service.entity.BookRecordListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/book_record")
public class BookRecordController {

    @Resource
    BookRecordService bookRecordService;

    /**
     * 查询预约登记记录
     *
     * @param elderName        老人姓名
     * @param bookerName       预约者姓名
     * @param startBookingDate 预约登记时间开始日期
     * @param endBookingDate   预约登记时间结束日期
     * @param startCheckInDate 预约入住时间开始日期
     * @param endCheckInDate   预约入住时间结束日期
     * @param pageSize         页大小
     * @param current          当前页
     * @return BookRecordListResponseBody
     */
    @GetMapping("/query")
    public Result<BookRecordListResponseBody> queryBookRecords(
            @RequestParam(value = "elderName", required = false) String elderName,
            @RequestParam(value = "bookerName", required = false) String bookerName,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "startBookingDate", required = false) String startBookingDate,
            @RequestParam(value = "endBookingDate", required = false) String endBookingDate,
            @RequestParam(value = "startCheckInDate", required = false) String startCheckInDate,
            @RequestParam(value = "endCheckInDate", required = false) String endCheckInDate,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("elderName", elderName != null ? "%" + elderName + "%" : null);
        params.put("bookerName", bookerName != null ? "%" + bookerName + "%" : null);
        params.put("status", status);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        params.put("startBookingDate", startBookingDate);
        params.put("endBookingDate", endBookingDate);
        params.put("startCheckInDate", startCheckInDate);
        params.put("endCheckInDate", endCheckInDate);
        try {
            BookRecordListResponseBody res = bookRecordService.queryBookRecords(params);
            return Result.success(res);
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }


    /**
     * 新增预约登记的接口
     *
     * @param req BookRecordRequestBody.addBookRecord
     * @return null
     */
    @PostMapping("/add")
    public Result<?> addBookRecord(@Valid @RequestBody BookRecordRequestBody.AddBookRecord req) {
        BookRecord bookRecord = BookRecord.builder()
                .bookerName(req.getBookerName())
                .bookerPhone(req.getBookerPhone())
                .bookingDate(req.getBookingDate())
                .checkInDate(req.getCheckInDate())
                .elderName(req.getElderName())
                .elderCertificateId(req.getElderCertificateId())
                .elderNumber(req.getElderNumber())
                .nurseId(req.getNurseId())
                .selfCareSituation(req.getSelfCareSituation())
                .status(0)
                .build();
        try {
            boolean flag = bookRecordService.save(bookRecord);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("add BookRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 修改预约登记的接口
     *
     * @param req BookRecordRequestBody.updateBookRecord
     * @return null
     */
    @PutMapping("/update")
    public Result<?> updateBookRecord(@Valid @RequestBody BookRecordRequestBody.UpdateBookRecord req) {
        BookRecord bookRecord = BookRecord.builder()
                .id(req.getId())
                .bookerName(req.getBookerName())
                .bookerPhone(req.getBookerPhone())
                .bookingDate(req.getBookingDate())
                .checkInDate(req.getCheckInDate())
                .elderName(req.getElderName())
                .elderCertificateId(req.getElderCertificateId())
                .elderNumber(req.getElderNumber())
                .nurseId(req.getNurseId())
                .selfCareSituation(req.getSelfCareSituation())
                .status(req.getStatus())
                .build();
        try {
            boolean flag = bookRecordService.updateById(bookRecord);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("update BookRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 删除预约登记接口
     *
     * @param id 预约登记记录的id
     * @return null
     */
    @DeleteMapping("/delete")
    public Result<?> deleteBookRecord(@RequestParam("id") @NotEmpty Integer id) {
        try {
            boolean flag = bookRecordService.removeById(id);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("delete BookRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
